from flask import Flask, render_template, jsonify, request
from datetime import timedelta
from get_data_fruit import *
from flask_cors import CORS

# 实例化app
app = Flask(__name__)
# 引入配置文件
app.config.from_pyfile("settings.py")
# 配置缓存最大时间
app.send_file_max_age_default = timedelta(seconds=1)
# 配置session有效期
app.config['PERMANENT_SESSION_LIFETIME'] = timedelta(seconds=1)
# 开启跨域
CORS(app)

# 注册路由
# TODO 全部任务数据接口 --------------------------------------------------------------------------------------------------
@app.route('/')
def data_api():
    tasks = list()
    tasks.extend(['task1a','task1b','task2','task3a','task3b'])
    tasks.extend([f'task{i}' for i in range(4, 6)])
    tasks.extend(['task6a', 'task6b'])
    tasks.extend([f'task{i}' for i in range(7, 9)])
    return render_template('data_api.html', tasks=tasks, markets=markets())

# TODO task0 超市名 ------------------------------------------------------------------------------------------------------
@app.route('/data-api/task0', methods=['POST', 'GET'])
def t0():
    market_names = markets()
    return jsonify(market_names)

# TODO task1a 散点图 ------------------------------------------------------------------------------------------------------
@app.route('/data-api/task1a', methods=['POST', 'GET'])
def t1a():
    market_name = request.values.get('market_name')
    data = task1a(market_name)
    return jsonify(data)

# TODO task1b 词云图 ------------------------------------------------------------------------------------------------------
@app.route('/data-api/task1b', methods=['POST', 'GET'])
def t1b():
    market_name = request.values.get('market_name')
    data = task1b(market_name)
    return jsonify(data)

# TODO task2 关系图 ------------------------------------------------------------------------------------------------------
@app.route('/data-api/task2', methods=['POST', 'GET'])
def t2():
    market_name = request.values.get('market_name')
    data = task2(market_name)
    return jsonify(data)

# TODO task3a 箱线图-左 ------------------------------------------------------------------------------------------------------
@app.route('/data-api/task3a', methods=['POST', 'GET'])
def t3a():
    market_name = request.values.get('market_name')
    data = task3a(market_name)
    return jsonify(data)

# TODO task3b 箱线图-右 ------------------------------------------------------------------------------------------------------
@app.route('/data-api/task3b', methods=['POST', 'GET'])
def t3b():
    market_name = request.values.get('market_name')
    data = task3b(market_name)
    return jsonify(data)

# TODO task4 中上-数据展示 ------------------------------------------------------------------------------------------------------
@app.route('/data-api/task4', methods=['POST', 'GET'])
def t4():
    market_name = request.values.get('market_name')
    data = task4(market_name)
    return jsonify(data)

# TODO task5 地图 ------------------------------------------------------------------------------------------------------
@app.route('/data-api/task5', methods=['POST', 'GET'])
def t5():
    # post请求：根据取请求的参数name，获取卖场的数据
    market_name = request.values.get('market_name')
    data = task5(market_name)
    return jsonify(data)

# TODO task6a 气泡图 ------------------------------------------------------------------------------------------------------
@app.route('/data-api/task6a', methods=['POST', 'GET'])
def t6a():
    market_name = request.values.get('market_name')
    data = task6a(market_name)
    return jsonify(data)

# TODO task6 中下 ------------------------------------------------------------------------------------------------------
@app.route('/data-api/task6b', methods=['POST', 'GET'])
def t6b():
    market_name = request.values.get('market_name')
    data = task6b(market_name)
    return jsonify(data)

# TODO task7 仪表盘 ------------------------------------------------------------------------------------------------------
@app.route('/data-api/task7', methods=['POST', 'GET'])
def t7():
    market_name = request.values.get('market_name')
    data = task7(market_name)
    return jsonify(data)

# TODO task8 折线图-会员 ------------------------------------------------------------------------------------------------------
@app.route('/data-api/task8', methods=['POST', 'GET'])
def t8():
    market_name = request.values.get('market_name')
    data = task8(market_name)
    return jsonify(data)

if __name__ == '__main__':
    app.run(port=5000)